package com.twentyOne.ClothingLand.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.twentyOne.ClothingLand.dao.IBarcodeDAO;
import com.twentyOne.ClothingLand.dao.IGoodsDAO;
import com.twentyOne.ClothingLand.dao.ISaleOrderDAO;
import com.twentyOne.ClothingLand.dao.ISaleOrderItemDAO;
import com.twentyOne.ClothingLand.dao.impl.BarcodeDAO;
import com.twentyOne.ClothingLand.dao.impl.GoodsDAO;
import com.twentyOne.ClothingLand.dao.impl.SaleOrderDAO;
import com.twentyOne.ClothingLand.dao.impl.SaleOrderItemDAO;
import com.twentyOne.ClothingLand.vo.Barcode;
import com.twentyOne.ClothingLand.vo.Goods;
import com.twentyOne.ClothingLand.vo.SaleOrder;
import com.twentyOne.ClothingLand.vo.SaleOrderItem;

public class SalesReportService {
	
	private ISaleOrderDAO saleOrderDAO = new SaleOrderDAO();
	private ISaleOrderItemDAO saleOrderItemDAO = new SaleOrderItemDAO();
	private IBarcodeDAO barcodeDAO = new BarcodeDAO();
	private IGoodsDAO goodsDAO = new GoodsDAO();
	
	
	public int searchTotalByYM(String year, String month) {
		
		List<SaleOrder> list = this.saleOrderDAO.searchSaleOrderByYM(year, month);
		double amount = 0;
		
		if(list == null) {
			return 0;
		}
		
		for (int i = 0; i < list.size(); i++) {
			amount += list.get(i).getTotal();
		}
		
		return (int)amount;
		
	}
	
	public int searchTotalByClass_YMD(String classId, String startDate, String endDate, int shopId) {
		
		double amount = 0;
		List<SaleOrder> SaleOrderList = this.saleOrderDAO.searchSaleOrderByYMD(startDate, endDate, shopId);
		if(SaleOrderList == null) {
			return 0;
		}
		
		for (int i = 0; i < SaleOrderList.size(); i++) {
			 List<SaleOrderItem> itemList = this.saleOrderItemDAO.searchItemBySaleOrderNo(SaleOrderList.get(i).getSaleOrderNo());
			 if (itemList != null) {
				for (int j = 0; j < itemList.size(); j++) {
					Barcode barcode = this.barcodeDAO.searchBarcodeByCode(itemList.get(j).getBarcode());
					Goods goods = this.goodsDAO.searchGoodsByID(barcode.getGoodsId());
					String[] classId_Goods = goods.getClassId().split(",");
					for (int k = 0; k < classId_Goods.length; k++) {
						if(classId_Goods[k].equals(classId)) {
							amount += itemList.get(j).getSubTotal();
						}
					}
				}
			}
		}
		
		return (int) amount;
		
	}
	
}
